/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.util.state.InvoiceAndReceiptState;
import Merlion.util.state.SalesDocumentState;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletException;
import javax.sql.DataSource;
import org.primefaces.model.StreamedContent;
import Merlion.util.state.LineEntryState;
import java.util.*;

/**
 *
 * @author Toshiba
 */
@ManagedBean(name = "jasperManagedBean")
@RequestScoped
public class jasperManagedBean {

    @Resource(name = "jasperjdbc")
    private DataSource jasperjdbc;
    private StreamedContent file;

    private InvoiceAndReceiptState selectedPrintInvoice;
    private SalesDocumentState selectedPrintDocument;
    public jasperManagedBean() {
    }

    public void generateCustomerReport(ActionEvent event) throws ServletException {
        try {
            System.out.print("managedbean1");
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/customerServlet");
            System.out.print("managedbean2");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void generateSalesLeadReport(ActionEvent event) throws ServletException {
        try {
            System.out.print("managedbean1");
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/salesLeadServlet");
            System.out.print("managedbean2");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void generateContactReport(ActionEvent event) throws ServletException {
        try {
            System.out.print("managedbean1");
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/contactServlet");
            System.out.print("managedbean2");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void generateCreditProfileReport(ActionEvent event) throws ServletException {
        try {
            System.out.print("managedbean1");
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/creditProfileServlet");
            System.out.print("managedbean2");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    
    public void generateInvoiceReport(ActionEvent event) throws ServletException{
                 System.out.println("TRY TO GENERATE REPORT ");
        try
        { 
            if(selectedPrintInvoice != null){
            System.out.print("managedbean1");
             Long selectedPrintInvoiceID = selectedPrintInvoice.getInvoiceId();
             FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedPrintInvoiceID", selectedPrintInvoiceID);
             FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/invoiceServlet");
            System.out.print("managedbean2");
            }else{
               System.out.println("selectedPrintInvoice is null"); 
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
        public void generatePurchaseOrderReport(ActionEvent event) throws ServletException{
                 System.out.println("TRY TO GENERATE REPORT ");
        try
        { 
            if(selectedPrintDocument != null){
            System.out.print("managedbean1");
           //  FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedPrintInquiry", selectedPrintInquiry);
             Long selectedPrintPuchaseOrderID = selectedPrintDocument.getId();
             FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedPrintPuchaseOrderID", selectedPrintPuchaseOrderID);
             FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/purchaseOrderServlet");
            System.out.print("managedbean2");
            }else{
               System.out.println("selectedPrintInquiry is null"); 
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
        
         public void generateReceiptReport(ActionEvent event) throws ServletException{
                 System.out.println("TRY TO GENERATE REPORT ");
        try
        { 
            if(selectedPrintInvoice != null){
            System.out.print("managedbean1");
             Long selectedPrintInvoiceID = selectedPrintInvoice.getInvoiceId();
             FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedPrintReceiptID", selectedPrintInvoiceID);
             FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8181/MerlionERP-war/receiptServlet");
            System.out.print("managedbean2");
            }else{
               System.out.println("selectedPrintReceipt is null"); 
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
   
    public InvoiceAndReceiptState getSelectedPrintInvoice() {
        return selectedPrintInvoice;
    }

    /**
     * @param selectedPrintInvoice the selectedPrintInvoice to set
     */
    public void setSelectedPrintInvoice(InvoiceAndReceiptState selectedPrintInvoice) {
        this.selectedPrintInvoice = selectedPrintInvoice;
    }
}
